package cn.com.backstage.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_user_admin;
import cn.com.api.entity.ApiU_discover;
import cn.com.api.entity.ApiU_discover_cate;
import cn.com.api.entity.ApiU_discover_spec;
import cn.com.api.entity.ApiU_message;
import cn.com.api.service.ApiU_discoverService;
import cn.com.api.service.ApiU_discover_cateService;
import cn.com.api.service.ApiU_discover_specService;
import cn.com.api.service.ApiU_messageService;
import cn.com.backstage.controller.base.U_discoverBaseController;
import cn.com.util.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class U_discoverController  extends U_discoverBaseController {

	private static Logger logger = Logger.getLogger(U_discoverController.class);
	
	@Autowired
	private ApiU_discoverService apiU_discoverService;
	
	@Autowired
	private ApiU_discover_cateService apiU_discover_cateService;
	
	@Autowired
	private ApiU_discover_specService apiU_discover_specService;
	
	@Autowired
	private ApiU_messageService apiU_messageService;
	
	/**
	 * 审核新发现
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "u_activity_discover-doverify")
	public void u_activity_discover_doverify(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String, Object> param = new HashMap<String, Object>();
		if(request.getParameter("id") != null){
			Integer id = Integer.valueOf(request.getParameter("id"));
			
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			int type = u_discover.getType();			
			String activity_type = u_discover.getActivity_type();
			
			
			if (u_discover.getStatus() == 1) {
				ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
				
				u_discover.setIs_verify(1);
				u_discover.setVerify_by(admin.getUser_id().toString());
				u_discover.setVerify_time(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				
				this.apiU_discoverService.U_discoverVerify(u_discover);
			}
			
		
			//TODO 附近服务和需求，获得可以接单的用户列表插入数据库
			if(type == 4){
				param.put("type", type);
				param.put("activity_type", activity_type);
				param.put("id", id);
				this.apiU_discoverService.findTaskInfo(param);			
			}
			
		}
	}
	
	/**
	 * 拒绝新发现活动
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "u_activity_discover-dodenie")
	public void u_activity_discover_dodenie(HttpServletRequest request, HttpServletResponse response) throws Exception{
		if(request.getParameter("id") != null){
			Integer id = Integer.valueOf(request.getParameter("id"));
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			
			if (u_discover.getStatus() == 1) {
				ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
				
				u_discover.setIs_verify(2);
				u_discover.setVerify_by(admin.getUser_id().toString());
				u_discover.setVerify_time(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				
				this.apiU_discoverService.U_discoverVerify(u_discover);
			}
		}
	}
	
	
	/**
	 * 保存新发现服务
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "service_discover-add")
	public void service_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cateId = request.getParameter("cateId");
		String headImg = request.getParameter("head_img");
		String image_list = request.getParameter("image_list");
		String image_path = request.getParameter("image_paths");
		
		
		String imagsResult = "";
		if(image_path != null  && !"".equals(image_path)){
			imagsResult = image_path;
		}
		if(image_list!= null && !"".equals(image_list)){
			//处理多个海报图片
			imagsResult = image_list;		
			if(imagsResult.length() >= 1 && (",").equals(imagsResult.substring(0,1))){
				imagsResult = imagsResult.substring(1, imagsResult.length());
			}
		}
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");
		String isAdd = request.getParameter("is_add");
		String discoverId = request.getParameter("id");
		String regioninfo = request.getParameter("regioninfo");
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");

		Map<String, Object> map = new HashMap<String, Object>();

		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				/**add start*/
				//apiU_discover.setHead_img(headImg);				
				apiU_discover.setHead_img(imagsResult);
				/**add end*/
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(1);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}

				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i = 0; i < chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();

					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));

					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);

					apiU_discover_spec.setSort(i + 1);
					apiU_discover_spec.setType(1);

					specs.add(apiU_discover_spec);
				}

				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();

				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				/**add start*/
				//apiU_discover.setHead_img(headImg);				
				apiU_discover.setHead_img(imagsResult);
				/**add end*/
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(1);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}

				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i = 0; i < chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();

					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));

					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);

					apiU_discover_spec.setSort(i + 1);
					apiU_discover_spec.setType(1);

					specs.add(apiU_discover_spec);
				}

				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 新增或修改时，验证新发现活动或服务的字段
	 * @param request
	 * @return
	 */
	private boolean validateAddActivityDiscover(HttpServletRequest request) {
		String cateId = request.getParameter("cateId");
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");

		if (cateId.equalsIgnoreCase("")) {
			return false;
		}

		if (vedio.equalsIgnoreCase("")) {
			return false;
		}

		if (title.equalsIgnoreCase("")) {
			return false;
		}

		if (address.equalsIgnoreCase("")) {
			return false;
		}

		if (beginTime.equalsIgnoreCase("")) {
			return false;
		}

		if (endTime.equalsIgnoreCase("")) {
			return false;
		}

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			if (sdf.parse(beginTime).after(sdf.parse(endTime))) {
				return false;
			}

			if (chargenames == null) {
				return false;
			} else {
				for (int i = 0; i < chargenames.length; i++) {
					if (chargenames[i].length() == 0) {
						return false;
					}

					if (Float.valueOf(chargefees[i]) < 0) {
						return false;
					}

					if (Integer.valueOf(chargelimits[i]) < 0) {
						return false;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	/**
	 * 添加或修改保存新发现活动
	 */
	@RequestMapping(value = "activity_discover-add")
	public void u_activity_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cateId = request.getParameter("cateId");
		String headImg = request.getParameter("head_img");
		String vedio = request.getParameter("vedio");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String[] chargenames = request.getParameterValues("chargenames");
		String[] chargefees = request.getParameterValues("chargefees");
		String[] chargelimits = request.getParameterValues("chargelimits");
		String regioninfo = request.getParameter("regioninfo");
		String discoverId = request.getParameter("id");
		/*String image_list = request.getParameter("image_list");
		String image_path = request.getParameter("image_paths");
		
		
		String imagsResult = "";
		if(image_path != null  && !"".equals(image_path)){
			imagsResult = image_path;
		}
		if(image_list!= null && !"".equals(image_list)){
			//处理多个海报图片
			imagsResult = image_list;
			if(imagsResult.length() >= 1 && (",").equals(imagsResult.substring(0,1))){
				imagsResult = imagsResult.substring(1, imagsResult.length());
			}
		}*/
		Map<String, Object> map = new HashMap<String, Object>();
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();

				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(0);

				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i = 0; i < chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();

					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));

					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);

					apiU_discover_spec.setSort(i + 1);
					apiU_discover_spec.setType(0);

					specs.add(apiU_discover_spec);
				}

				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddActivityDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();

				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setActivity_type(cateId);
				apiU_discover.setAddress(address);
				apiU_discover.setVedio(vedio);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(headImg);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setCity(regioninfo);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(0);

				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();
				for (int i = 0; i < chargenames.length; i++) {
					ApiU_discover_spec apiU_discover_spec = new ApiU_discover_spec();

					apiU_discover_spec.setPrice_name(chargenames[i]);
					apiU_discover_spec.setPrice(new BigDecimal(chargefees[i]));

					apiU_discover_spec.setLimit(Integer.valueOf(chargelimits[i]));
					apiU_discover.setPeople_limit(chargelimits[i]);

					apiU_discover_spec.setSort(i + 1);
					apiU_discover_spec.setType(0);

					specs.add(apiU_discover_spec);
				}

				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	
	/**
	 * 保存新发现需求
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "requirement_discover-add")
	public void u_requirement_discover_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String headImg = request.getParameter("head_img");
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");
		String description = request.getParameter("description");
		String isAdd = request.getParameter("is_add");

		String discoverId = request.getParameter("id");
		String image_list = request.getParameter("image_list");
		String image_path = request.getParameter("image_paths");
		
		
		String imagsResult = "";
		if(image_path != null  && !"".equals(image_path)){
			imagsResult = image_path;
		}
		if(image_list!= null && !"".equals(image_list)){
			//处理多个海报图片
			imagsResult = image_list;
			if(imagsResult.length() >= 1 && (",").equals(imagsResult.substring(0,1))){
				imagsResult = imagsResult.substring(1, imagsResult.length());
			}
		}
		Map<String, Object> map = new HashMap<String, Object>();
		ApiT_user_admin admin = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
		if (discoverId.equalsIgnoreCase("")) {
			if (this.validateAddRequirementDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();

				apiU_discover.setAddress(address);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(imagsResult);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(2);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}
				System.out.println("yesyesye");
				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();

				this.apiU_discoverService.U_discoverInsertionWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		} else {
			if (this.validateAddRequirementDiscover(request)) {
				ApiU_discover apiU_discover = new ApiU_discover();

				apiU_discover.setId(Integer.valueOf(discoverId));
				apiU_discover.setAddress(address);
				apiU_discover.setTitle(title);
				apiU_discover.setHead_img(imagsResult);
				apiU_discover.setBegin_time(beginTime);
				apiU_discover.setEnd_time(endTime);
				apiU_discover.setDescription(description);
				apiU_discover.setIs_verify(0);
				apiU_discover.setIs_delete(0);
				apiU_discover.setIs_cancel(0);
				apiU_discover.setAdd_time(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
				apiU_discover.setUser_id(admin.getUser_id()+"");// 暂时设置
				apiU_discover.setType(2);
				if (isAdd == null) {
					apiU_discover.setIs_add(0);
				} else {
					apiU_discover.setIs_add(1);
				}

				List<ApiU_discover_spec> specs = new ArrayList<ApiU_discover_spec>();

				this.apiU_discoverService.U_discoverUpdateWithSpec(apiU_discover, specs);

				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "修改成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "传入数据不正确！");
			}
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 验证新增或修改新发现需求
	 * @param request
	 * @return
	 */
	private boolean validateAddRequirementDiscover(HttpServletRequest request) {
		String title = request.getParameter("title");
		String address = request.getParameter("address");
		String beginTime = request.getParameter("begin_time");
		String endTime = request.getParameter("end_time");

		if (title.equalsIgnoreCase("")) {
			return false;
		}

		if (address.equalsIgnoreCase("")) {
			return false;
		}

		if (beginTime.equalsIgnoreCase("")) {
			return false;
		}

		if (endTime.equalsIgnoreCase("")) {
			return false;
		}

		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			if (sdf.parse(beginTime).after(sdf.parse(endTime))) {
				return false;
			}

		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	
	
	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "activity_discover-list")
	public ModelAndView u_activity_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_list.jsp");

		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(0);
		Map conditions = new HashMap();

		String title = request.getParameter("activity_name");
		String cateId = request.getParameter("cateId");
		String status = request.getParameter("status");
		String aid = request.getParameter("aid");

		if (cateId != null && !cateId.equalsIgnoreCase("-1")) {
			conditions.put("cate_id", cateId);
		}
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", new String(title.getBytes("iso-8859-1"), "utf-8"));
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "0");
		if (aid != null) {
			conditions.put("aid", aid);
		}
		List<Map<String, Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("aid", aid);
		return mv;
	}
	
	
	/**
	 * 新发现需求列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "requirement_discover-list")
	public ModelAndView u_requirement_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_requirement_list.jsp");

		Map conditions = new HashMap();

		String title = request.getParameter("activity_name");
		String status = request.getParameter("status");
		String aid = request.getParameter("aid");
		
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", new String(title.getBytes("iso-8859-1"), "utf-8"));
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "2");
		if (aid != null) {
			conditions.put("aid", aid);
		}
		List<Map<String, Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("aid", aid);
		return mv;
	}
	
	
	@RequestMapping(value = "requirement_about_discover-list")
	public ModelAndView u_requirement_about_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_requirement_list.jsp");

		Map conditions = new HashMap();

		String title = request.getParameter("activity_name");
		String status = request.getParameter("status");
		String aid = request.getParameter("aid");
		
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", new String(title.getBytes("iso-8859-1"), "utf-8"));
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "4");
		if (aid != null) {
			conditions.put("aid", aid);
		}
		List<Map<String, Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		
		conditions.put("type", "2");
		List<Map<String, Object>> u_discovers_info = apiU_discoverService.searchDiscover(conditions, pageInfo);
		
		List<ApiU_discover> u_discover_result = new ArrayList<ApiU_discover>();
		for(int i = 0; i<u_discovers_info.size(); i++){
			u_discover_result.add((ApiU_discover) u_discovers_info.get(i));
		}
		
		for(int i = 0; i<u_discovers.size(); i++){
			u_discover_result.add((ApiU_discover) u_discovers.get(i));
		}
		mv.addObject("u_discovers", u_discover_result);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("aid", aid);
		return mv;
	}
	
	/**
	 * 新发现服务列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "service_discover-list")
	public ModelAndView u_service_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_service_list.jsp");

		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(1);
		Map conditions = new HashMap();

		String title = request.getParameter("activity_name");
		String cateId = request.getParameter("cateId");
		String status = request.getParameter("status");
		String aid = request.getParameter("aid");
		if (cateId != null && !cateId.equalsIgnoreCase("-1")) {
			conditions.put("cate_id", cateId);
		}
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", new String(title.getBytes("iso-8859-1"), "utf-8"));
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "1");
		if (aid != null) {
			conditions.put("aid", aid);
		}
		List<Map<String, Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discovers);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("aid", aid);
		return mv;
	}
	
	
	@RequestMapping(value = "service_about_discover-list")
	public ModelAndView u_about_service_discover_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_service_list.jsp");

		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(5);
		Map conditions = new HashMap();

		String title = request.getParameter("activity_name");
		String cateId = request.getParameter("cateId");
		String status = request.getParameter("status");
		String aid = request.getParameter("aid");
		if (cateId != null && !cateId.equalsIgnoreCase("-1")) {
			conditions.put("cate_id", cateId);
		}
		if (title != null && !title.equalsIgnoreCase("")) {
			conditions.put("title", new String(title.getBytes("iso-8859-1"), "utf-8"));
		}
		if (status != null && !status.equalsIgnoreCase("-1")) {
			conditions.put("status", status);
		}
		conditions.put("type", "5");
		if (aid != null) {
			conditions.put("aid", aid);
		}
		
		List<Map<String, Object>> u_discovers = apiU_discoverService.searchDiscover(conditions, pageInfo);
		conditions.put("type", "1");
		
		List<Map<String, Object>> u_discovers_info = apiU_discoverService.searchDiscover(conditions, pageInfo);
		
		List<ApiU_discover> u_discover_result = new ArrayList<ApiU_discover>();
		for(int i = 0; i<u_discovers_info.size(); i++){
			u_discover_result.add((ApiU_discover) u_discovers_info.get(i));
		}
		
		for(int i = 0; i<u_discovers.size(); i++){
			u_discover_result.add((ApiU_discover) u_discovers.get(i));
		}
		
		mv.addObject("cates", cates);
		mv.addObject("u_discovers", u_discover_result);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("aid", aid);
		return mv;
	}
	
	/**
	 * 审核新发现活动
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "u_activity_discover-verify")
	public ModelAndView u_activity_discover_verify(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_discover_verify.jsp");
		//
		if (request.getParameter("id") != null) {
			Integer id = Integer.valueOf(request.getParameter("id"));
			ApiU_discover u_discover = apiU_discoverService.findU_discoverById(id);
			mv.addObject("u_discover", u_discover);

			List<ApiU_discover_spec> specs = this.apiU_discover_specService
					.getApiU_discover_specsBydiscover_Id(request.getParameter("id"));
			mv.addObject("specs", specs);
		}

		List<ApiU_discover_cate> cates = this.apiU_discover_cateService.findU_discover_byType(0);

		mv.addObject("cates", cates);

		return mv;
	}
	
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	
	@RequestMapping(value = "u_discover-delete")
	public void u_discover_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		
		//delete
         int ret =  apiU_discoverService.U_discover_delete(Integer.valueOf(id));
		//
		if(ret > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 * 取消
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	
	@RequestMapping(value = "u_discover-cancel")
	public void u_discover_cancel(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		
		//cancel
         int ret =  apiU_discoverService.U_discovercancel(Integer.valueOf(id));
		//
		if(ret > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "取消成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}



